package date;

import org.junit.Test;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;
import java.util.stream.Collectors;

/**
 * @author
 * @Title: DateTest
 * @Description:
 * @date 2023/10/9 16:01
 */
public class DateTest {

    /**
     * 将当前时间转为日本时区
     */
    @Test
    public void test01() throws Exception{
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        sdf.setTimeZone(TimeZone.getTimeZone("Asia/Tokyo"));
        String jpTime = sdf.format(date);
        System.out.println(jpTime);


        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date result = sdf2.parse(jpTime);
        System.out.println(result); // 这里的result其实还是中国时间,只不过比new Date 往后推了1小时
    }

    @Test
    public void test03() throws ParseException {
        String dateString = "2023-10-27T14:30:00+01:00"; // 包含 +0800 时区偏移量
        DateTimeFormatter isoZonedDateTimeFormatter = DateTimeFormatter.ISO_ZONED_DATE_TIME;
        ZonedDateTime zonedDateTime = ZonedDateTime.parse(dateString, isoZonedDateTimeFormatter);
        System.out.println("ISO 8601 (带时区ID) 的 ZonedDateTime: " + zonedDateTime);
        ZoneId zoneId = zonedDateTime.getZone();  // 获取时区 ID (例如，Asia/Shanghai)
        System.out.println("时区 ID: " + zoneId);
        ZoneOffset offset = zonedDateTime.getOffset(); // 获取时区偏移量
        System.out.println("时区偏移量: " + offset);

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX");
        sdf.setTimeZone(TimeZone.getTimeZone("JST"));
        Date parse = sdf.parse(dateString);
        long time = parse.getTime();
        System.out.println(time);


        sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX");
        sdf.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
        parse = sdf.parse(dateString);
        long time2 = parse.getTime();
        System.out.println(time2);




        System.out.println(sdf.format(new Date(time)));
    }


    /**
     * 获取 2023-12-30 - 2024-02-10之间的日期(闭区间)
     *  DateTimeFormatter.ofPattern("yyyy-MM-dd") 是线程安全的
     */
    @Test
    public void getBetweenDateTime(){
        String begin = "2023-12-30";
        String end = "2024-02-10";

        LocalDate beginDate = LocalDate.parse(begin, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        LocalDate endDate = LocalDate.parse(end, DateTimeFormatter.ofPattern("yyyy-MM-dd"));

        List<LocalDate> localDateList = new ArrayList<>();


        long length = endDate.toEpochDay() - beginDate.toEpochDay() + 1; // 这里不包含截止所以要加1
        for(int i = 0; i < length; i++){
            localDateList.add(beginDate.plusDays(i));
        }

        List<String> collect = localDateList.stream().map(e -> e.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"))).collect(Collectors.toList());
        System.out.println(collect);
    }

}
